Write a function:

def solution(A)

that, given a non-empty zero-indexed array A of N integers, returns the minimal difference that can be achieved.

For example, given: A[0] = 3 A[1] = 1 A[2] = 2 A[3] = 4 A[4] = 3

the function should return 1, as explained above.

Assume that:

    N is an integer within the range [2..100,000];
    each element of array A is an integer within the range [−1,000..1,000].

Complexity:

    expected worst-case time complexity is O(N);
    expected worst-case space complexity is O(N), beyond input storage (not counting the storage required for input arguments).

Elements of input arrays can be modified.

Defining the Function:


left_side_sum = P[0:N-1]

right_side_sum = P[N:]


Difference = absolute(left_side_sum - right_side_sum)


Range:

N = [2,100,000]

P = [-1000, 1000]


Complexity

Time = O^n

Space = O^n


Answer:

Minimum_Difference



In [1]:
A = [3,1,2,4,3]

V1. broken Testing. Changing the iteration values to be used.

Designing the function


In [2]:
def tape_int_1(data):
    
    left_side_sum = 0
    
    right_side_sum = sum(data[:])
    
    min_diff = abs(left_side_sum - right_side_sum)
    
    print "\n initial min_diff: ", min_diff, 
    
    print "\ninitial left side sum: ", left_side_sum
    
    print "initial right side sum: ", right_side_sum , "\n"
    
    for counter,num in enumerate(data):
        if counter == 0: continue
        pre_num = data[counter-1]
        
        left_side_sum += pre_num
        
        right_side_sum -= pre_num
        
        right_side_sum += num
        
        print "\n pre_num: ", pre_num
        
        print "current num: ", num
        
        print "left_sum: ", left_side_sum
        
        print "right_sum: ", right_side_sum, "\n"
        
        current_diff = abs( left_side_sum - right_side_sum)
                           
        print "current diff: ", current_diff, "\n"
        
        if min_diff > current_diff:
            min_diff = current_diff
        
        print "\n current min_diff: ", min_diff
        
        #print "pre_num: ", pre_num
        
        #print "current num: ", num
        
        #print "left_sum: ", left_side_sum
        
        #print "right_sum: ", right_side_sum
                
        #print "current diff: ", current_diff, "\n"
        
        if counter >= len(data) -1:
            return min_diff

In [3]:
print A

print tape_int_1(A)


[3, 1, 2, 4, 3]

 initial min_diff:  13 
initial left side sum:  0
initial right side sum:  13 


 pre_num:  3
current num:  1
left_sum:  3
right_sum:  11 

current diff:  8 


 current min_diff:  8

 pre_num:  1
current num:  2
left_sum:  4
right_sum:  12 

current diff:  8 


 current min_diff:  8

 pre_num:  2
current num:  4
left_sum:  6
right_sum:  14 

current diff:  8 


 current min_diff:  8

 pre_num:  4
current num:  3
left_sum:  10
right_sum:  13 

current diff:  3 


 current min_diff:  3
3

V2.


In [4]:
def tape_int_2(data):
    
    left_side_sum = 0
    
    right_side_sum = sum(data[:])
    
    min_diff = abs(left_side_sum - right_side_sum)
    
    print "\n initial min_diff: ", min_diff, 
    
    print "\ninitial left side sum: ", left_side_sum
    
    print "initial right side sum: ", right_side_sum , "\n"
    
    for num in data:
        #if counter == 0: continue
        #pre_num = data[counter-1]
        
        left_side_sum += num
        
        right_side_sum -= num
        
        #right_side_sum += num
        
        #print "\n pre_num: ", num
        
        print "current num: ", num
        
        print "left_sum: ", left_side_sum
        
        print "right_sum: ", right_side_sum, "\n"
        
        current_diff = abs( left_side_sum - right_side_sum)
                           
        print "current diff: ", current_diff, "\n"
        
        if min_diff > current_diff:
            min_diff = current_diff
        
        print "\n current min_diff: ", min_diff
        
        #print "pre_num: ", pre_num
        
        #print "current num: ", num
        
        #print "left_sum: ", left_side_sum
        
        #print "right_sum: ", right_side_sum
                
        #print "current diff: ", current_diff, "\n"
        
        #if counter >= len(data) -1:
    return min_diff

In [5]:
print A
print tape_int_2(A)


[3, 1, 2, 4, 3]

 initial min_diff:  13 
initial left side sum:  0
initial right side sum:  13 

current num:  3
left_sum:  3
right_sum:  10 

current diff:  7 


 current min_diff:  7
current num:  1
left_sum:  4
right_sum:  9 

current diff:  5 


 current min_diff:  5
current num:  2
left_sum:  6
right_sum:  7 

current diff:  1 


 current min_diff:  1
current num:  4
left_sum:  10
right_sum:  3 

current diff:  7 


 current min_diff:  1
current num:  3
left_sum:  13
right_sum:  0 

current diff:  13 


 current min_diff:  1
1

For example, given: A[0] = 3 A[1] = 1 A[2] = 2 A[3] = 4 A[4] = 3

the function should return 1, as explained above.

Initial Test complete.

V2.1


In [6]:
def tape_int_2_1(data):
    
    left_side_sum = 0
    
    right_side_sum = sum(data[:])
    
    min_diff = abs(left_side_sum - right_side_sum)
    
    for num in data:
        left_side_sum += num
        
        right_side_sum -= num
        
        current_diff = abs( left_side_sum - right_side_sum)
                           
        if min_diff > current_diff:
            min_diff = current_diff
        
    return min_diff

Testing - the code

Empty List


In [7]:
empty_list  = list()
print empty_list


[]

In [8]:
print empty_list

print tape_int_2_1(empty_list)


[]
0

Designing Data function for testing


In [9]:
def random_test_data(start,stop,size):
    import random
    
    return random.sample(xrange(start,stop), size)

In [10]:
small_simple_data = random_test_data(0,10,10)

print small_simple_data

print type(small_simple_data)


[0, 3, 4, 6, 2, 9, 7, 1, 5, 8]
<type 'list'>

In [11]:
print tape_int_2_1(small_simple_data)


3

V_2_2 test cases


In [12]:
double_list = [1,2]

print double_list

print tape_int_2_1(double_list)


[1, 2]
1

In [13]:
def tape_int_2_2(data):
    
    left_side_sum = 0
    
    right_side_sum = sum(data[:])
    
    print "\ninitial left side sum: ", left_side_sum
    
    print "initial right side sum: ", right_side_sum , 
    
    min_diff = abs(left_side_sum - right_side_sum)
    
    print "\n initial min_diff: ", min_diff, "\n"
    
    for i,num in enumerate(data):
        if i == len(data) - 1: break
        
        left_side_sum += num
        
        right_side_sum -= num
        
        print "current num: ", num
        
        print "left_sum: ", left_side_sum
        
        print "right_sum: ", right_side_sum, "\n"
        
        current_diff = abs( left_side_sum - right_side_sum)
        
        print "current diff: ", current_diff, "\n"
                           
        if min_diff > current_diff:
            min_diff = current_diff
            print "min diff: ", min_diff
        
    return min_diff

In [14]:
print double_list

print tape_int_2_2(double_list)


[1, 2]

initial left side sum:  0
initial right side sum:  3 
 initial min_diff:  3 

current num:  1
left_sum:  1
right_sum:  2 

current diff:  1 

min diff:  1
1

In [15]:
double_list_1 = [-2000,2000]

print double_list

print tape_int_2_2(double_list_1)


[1, 2]

initial left side sum:  0
initial right side sum:  0 
 initial min_diff:  0 

current num:  -2000
left_sum:  -2000
right_sum:  2000 

current diff:  4000 

0

V2_3


In [16]:
def tape_int_2_3(data):
    
    if len(data) == 0:
        return 0
    
    elif len(data) == 1:
        left_side_sum = 0
        
        right_side_sum = data[0]
    else:
    
        left_side_sum = data[0]
        
        right_side_sum = sum(data[1:])
        
    print "\ninitial left side sum: ", left_side_sum
    
    print "initial right side sum: ", right_side_sum , 
    
    min_diff = abs(left_side_sum - right_side_sum)
    
    print "\n initial min_diff: ", min_diff, "\n"
    
    pre_num = 0
    
    for i,num in enumerate(data):
        if i < 2:
            pre_num = num
            continue
        
        left_side_sum += pre_num
        
        right_side_sum -= pre_num
        
        #right_side_sum += num
        
        print "current num: ", num
        
        print "left_sum: ", left_side_sum
        
        print "right_sum: ", right_side_sum, "\n"
        
        current_diff = abs( left_side_sum - right_side_sum)
        
        print "current diff: ", current_diff, "\n"
        
        pre_num = num
                           
        if min_diff > current_diff:
            min_diff = current_diff
            print "min diff: ", min_diff
            
    return min_diff

In [17]:
double_list_1 = [-2000,2000]

print double_list

print tape_int_2_3(double_list_1)


[1, 2]

initial left side sum:  -2000
initial right side sum:  2000 
 initial min_diff:  4000 

4000

In [18]:
print tape_int_2_3(empty_list)


0

In [19]:
print A

print tape_int_2_3(A)


[3, 1, 2, 4, 3]

initial left side sum:  3
initial right side sum:  10 
 initial min_diff:  7 

current num:  2
left_sum:  4
right_sum:  9 

current diff:  5 

min diff:  5
current num:  4
left_sum:  6
right_sum:  7 

current diff:  1 

min diff:  1
current num:  3
left_sum:  10
right_sum:  3 

current diff:  7 

1

In [20]:
single_list = random_test_data(-2000,2000,1)

print single_list

print (len(single_list))

print tape_int_2_3(single_list)


[-1655]
1

initial left side sum:  0
initial right side sum:  -1655 
 initial min_diff:  1655 

1655

In [21]:
def tape_int_2_3_1(data):
    
    if len(data) == 0:
        return 0
    
    elif len(data) == 1:
        left_side_sum = 0
        
        right_side_sum = data[0]
    else:
    
        left_side_sum = data[0]
        
        right_side_sum = sum(data[1:])
    
    min_diff = abs(left_side_sum - right_side_sum)
    
    pre_num = 0
    
    for i,num in enumerate(data):
        if i < 2:
            pre_num = num
            continue
        
        left_side_sum += pre_num
        
        right_side_sum -= pre_num
        
        current_diff = abs( left_side_sum - right_side_sum)
        
        pre_num = num
                           
        if min_diff > current_diff:
            min_diff = current_diff
            
    return min_diff

In [22]:
def gen_list(start, stop ,size):
    from random import randint
    
    values_list = list()
    
    for counter in xrange(size):
        values_list.append(randint(start,stop))
    
    return values_list

In [23]:
def test_cases_fn(test_cases_list = None):
    
    if test_cases_list is None:
        test_cases_list = list()
        
    # zero element list
    test_cases_list.append(random_test_data(-1000,1000,0))
    
    #single element list
    test_cases_list.append(random_test_data(-1000,1000,1))
    
    #double element list
    test_cases_list.append(random_test_data(-1000,1000,2))
    
    #simple case list positive
    test_cases_list.append(random_test_data(0,1000,10))
    
    #simple case list negative
    test_cases_list.append(random_test_data(-1000,0,10))
    
    #simple case list all
    test_cases_list.append(random_test_data(-1000,1000,10))
    
    #medium case list positive
    test_cases_list.append(random_test_data(0,1000,100))
    
    #medium case list negative
    test_cases_list.append(random_test_data(-1000,0,100))
    
    #medium case list all
    test_cases_list.append(random_test_data(-1000,1000,100))
    
    #large case list positive
    test_cases_list.append(random_test_data(0,1000,1000))
    
    #large case list negative
    test_cases_list.append(random_test_data(-1000,0,1000))
    
    #large case list all
    test_cases_list.append(random_test_data(-1000,1000,1000))
    
    #extra large case list positive
    test_cases_list.append(random_test_data(0,10000,10000))
    
    #extra large case list negative
    test_cases_list.append(random_test_data(-10000,0,10000))
    
    #extra large case list all
    test_cases_list.append(random_test_data(-10000,10000,10000))
    
    #binary case list positive
    test_cases_list.append(gen_list(0,1,10))
    
    #binary case list negative
    test_cases_list.append(gen_list(-1,0,10))
    
    #binary case list all
    test_cases_list.append(gen_list(-1,1,10))
    
    #simple pyramid case list positive
    
    pyramid_arr = random_test_data(0,1000,10)
    test_cases_list.append( (list(reversed(pyramid_arr)))[:-1] + pyramid_arr )
    
    #simple pyramid case list negative
    pyramid_arr = random_test_data(-1000,0,10)
    test_cases_list.append( (list(reversed(pyramid_arr)))[:-1] + pyramid_arr )
    
    #simple pyramid case list all
    pyramid_arr = random_test_data(-1000,1000,10)
    test_cases_list.append( (list(reversed(pyramid_arr)))[:-1] + pyramid_arr )
    
    #medium pyramid case list positive
    pyramid_arr = random_test_data(0,1000,100)
    test_cases_list.append( (list(reversed(pyramid_arr)))[:-1] + pyramid_arr )
    
    #medium pyramid case list negative
    pyramid_arr = random_test_data(-1000,0,100)
    test_cases_list.append( (list(reversed(pyramid_arr)))[:-1] + pyramid_arr )
    
    #medium pyramid case list all
    pyramid_arr = random_test_data(-1000,1000,100)
    test_cases_list.append( (list(reversed(pyramid_arr)))[:-1] + pyramid_arr )
    
    #large pyramid case list positive
    pyramid_arr = random_test_data(0,1000,1000)
    test_cases_list.append( (list(reversed(pyramid_arr)))[:-1] + pyramid_arr )
    
    #large pyramid case list negative
    pyramid_arr = random_test_data(-1000,0,1000)
    test_cases_list.append( (list(reversed(pyramid_arr)))[:-1] + pyramid_arr )
    
    #large pyramid case list all
    pyramid_arr = random_test_data(-1000,1000,1000)
    test_cases_list.append( (list(reversed(pyramid_arr)))[:-1] + pyramid_arr )
    
    #simple 1 case list positive
    test_cases_list.append(gen_list(1,1,10))
    
    #simple 1 case list negative
    test_cases_list.append(gen_list(-1,-1,10))
    
    #medium 1 case list positive
    test_cases_list.append(gen_list(1,1,100))
    
    #medium 1 case list negative
    test_cases_list.append(gen_list(-1,1,100))
    
    #large 1 case list positive
    test_cases_list.append(gen_list(1,1,1000))
    
    #large 1 case list negative
    test_cases_list.append(gen_list(-1,-1,1000))
    
    #extra large 1 case list positive
    test_cases_list.append(gen_list(1,1,10000))
    
    #extra large 1 case list negative
    test_cases_list.append(gen_list(-1,-1,10000))

        
    return test_cases_list

In [24]:
print len(test_cases_fn())


35

In [25]:
print small_simple_data


[0, 3, 4, 6, 2, 9, 7, 1, 5, 8]

In [26]:
list_1 =  small_simple_data

print list_1

list_1 = list(reversed(list_1))

print list_1

print small_simple_data

pyramid = []
#pyramid = small_simple_data.sort(reverse=True)

pyramid = list_1[:-1] + small_simple_data

print pyramid


[0, 3, 4, 6, 2, 9, 7, 1, 5, 8]
[8, 5, 1, 7, 9, 2, 6, 4, 3, 0]
[0, 3, 4, 6, 2, 9, 7, 1, 5, 8]
[8, 5, 1, 7, 9, 2, 6, 4, 3, 0, 3, 4, 6, 2, 9, 7, 1, 5, 8]

In [27]:
pyramid_arr = random_test_data(0,1000,10)
 
print pyramid_arr
 
pyramid_test = (list(reversed(pyramid_arr)))[:-1] + pyramid_arr
 
print pyramid_test


[537, 509, 994, 931, 510, 700, 441, 477, 10, 488]
[488, 10, 477, 441, 700, 510, 931, 994, 509, 537, 509, 994, 931, 510, 700, 441, 477, 10, 488]

In [28]:
import itertools

#print list(itertools.product([-1, 1], repeat=5))

for n in xrange(2):
    print list(itertools.product([-1, 1], repeat=n))


[()]
[(-1,), (1,)]

In [29]:
def test_cases_multiprocess(process_fn,counter):
    
    counter = 10   

    from multiprocessing import Lock,Queue,Process, current_process, Manager
    
    
    if __name__ == '__main__':
        
        manager1 = Manager()
        
        queue_1 = Queue()
        
        test_cases_list = manager1.list()
        
        
           
        for count in xrange(counter):
            p_process_list = list()
            
            
            p_process = Process(target= test_cases_fn, args= (test_cases_list))
            
            p_process.start()
            
            p_process_list.append(p_process)
                
            p_counter = 0
       
        for count in xrange(counter):
            
            p_process_list[p_counter].join()
            
            p_counter += 1
            
                
                
    return test_cases_list

In [30]:
test_cases_fn_test = test_cases_fn()

len(test_cases_fn())

test_cases_mp = test_cases_multiprocess(test_cases_fn(),5)


---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-30-ef47ef872ca7> in <module>()
      3 len(test_cases_fn())
      4 
----> 5 test_cases_mp = test_cases_multiprocess(test_cases_fn(),5)

<ipython-input-29-1c9c1d7018aa> in test_cases_multiprocess(process_fn, counter)
     30         for count in xrange(counter):
     31 
---> 32             p_process_list[p_counter].join()
     33 
     34             p_counter += 1

IndexError: list index out of range

In [31]:
n = 3

print list(itertools.product([-1, 1], repeat=n))



print list(itertools.combinations('92516838962740',2))


[(-1, -1, -1), (-1, -1, 1), (-1, 1, -1), (-1, 1, 1), (1, -1, -1), (1, -1, 1), (1, 1, -1), (1, 1, 1)]
[('9', '2'), ('9', '5'), ('9', '1'), ('9', '6'), ('9', '8'), ('9', '3'), ('9', '8'), ('9', '9'), ('9', '6'), ('9', '2'), ('9', '7'), ('9', '4'), ('9', '0'), ('2', '5'), ('2', '1'), ('2', '6'), ('2', '8'), ('2', '3'), ('2', '8'), ('2', '9'), ('2', '6'), ('2', '2'), ('2', '7'), ('2', '4'), ('2', '0'), ('5', '1'), ('5', '6'), ('5', '8'), ('5', '3'), ('5', '8'), ('5', '9'), ('5', '6'), ('5', '2'), ('5', '7'), ('5', '4'), ('5', '0'), ('1', '6'), ('1', '8'), ('1', '3'), ('1', '8'), ('1', '9'), ('1', '6'), ('1', '2'), ('1', '7'), ('1', '4'), ('1', '0'), ('6', '8'), ('6', '3'), ('6', '8'), ('6', '9'), ('6', '6'), ('6', '2'), ('6', '7'), ('6', '4'), ('6', '0'), ('8', '3'), ('8', '8'), ('8', '9'), ('8', '6'), ('8', '2'), ('8', '7'), ('8', '4'), ('8', '0'), ('3', '8'), ('3', '9'), ('3', '6'), ('3', '2'), ('3', '7'), ('3', '4'), ('3', '0'), ('8', '9'), ('8', '6'), ('8', '2'), ('8', '7'), ('8', '4'), ('8', '0'), ('9', '6'), ('9', '2'), ('9', '7'), ('9', '4'), ('9', '0'), ('6', '2'), ('6', '7'), ('6', '4'), ('6', '0'), ('2', '7'), ('2', '4'), ('2', '0'), ('7', '4'), ('7', '0'), ('4', '0')]

In [32]:
perm = list(itertools.permutations((0,10,2)))

print perm

for items in perm:
    print items
    for element in items:
        print element


[(0, 10, 2), (0, 2, 10), (10, 0, 2), (10, 2, 0), (2, 0, 10), (2, 10, 0)]
(0, 10, 2)
0
10
2
(0, 2, 10)
0
2
10
(10, 0, 2)
10
0
2
(10, 2, 0)
10
2
0
(2, 0, 10)
2
0
10
(2, 10, 0)
2
10
0

In [33]:
from random import randint

print randint(0,10)


9

In [34]:
list_test = list()
size = 10

for counter in xrange(size):
    list_test.append(randint(-1,1))

print list_test


[1, -1, -1, -1, 0, -1, 0, 0, -1, 1]

In [35]:
print gen_list(-2000,2000,10)


[1945, 1197, -1196, -1089, 984, -713, -645, 1474, 976, -37]

Multiprocessing:


In [36]:
counter = 10   

from multiprocessing import Lock,Queue,Process, current_process, Manager



if __name__ == '__main__':
    
    manager1 = Manager()
    
    queue_1 = Queue()
    
    test_cases_list = manager1.list([[0]])
    
    
    p_process_list = list()
    for count in xrange(counter):
        #p_process_list = list()
        
        
        p_process = Process(target= test_cases_fn, args= (test_cases_list))
        
        p_process.start()
        
        p_process_list.append(p_process)
            
        p_counter = 0
        
        print len(p_process_list)
   
    for count in xrange(counter):
        
        print counter
        
        print p_counter
        
        print len(p_process_list)
        
        print p_process_list
        
        p_process_list[p_counter].join()
        
        p_counter += 1
        
        print test_cases_list


1
2
3
4
5
6
7
8
9
10
10
0
10
[<Process(Process-13, started)>, <Process(Process-14, started)>, <Process(Process-15, started)>, <Process(Process-16, started)>, <Process(Process-17, started)>, <Process(Process-18, started)>, <Process(Process-19, started)>, <Process(Process-20, started)>, <Process(Process-21, started)>, <Process(Process-22, started)>]
[[0]]
10
1
10
[<Process(Process-13, stopped)>, <Process(Process-14, stopped)>, <Process(Process-15, started)>, <Process(Process-16, started)>, <Process(Process-17, started)>, <Process(Process-18, started)>, <Process(Process-19, started)>, <Process(Process-20, started)>, <Process(Process-21, started)>, <Process(Process-22, started)>]
[[0]]
10
2
10
[<Process(Process-13, stopped)>, <Process(Process-14, stopped)>, <Process(Process-15, started)>, <Process(Process-16, started)>, <Process(Process-17, started)>, <Process(Process-18, started)>, <Process(Process-19, started)>, <Process(Process-20, started)>, <Process(Process-21, started)>, <Process(Process-22, started)>]
[[0]]
10
3
10
[<Process(Process-13, stopped)>, <Process(Process-14, stopped)>, <Process(Process-15, stopped)>, <Process(Process-16, stopped)>, <Process(Process-17, started)>, <Process(Process-18, started)>, <Process(Process-19, started)>, <Process(Process-20, started)>, <Process(Process-21, started)>, <Process(Process-22, started)>]
[[0]]
10
4
10
[<Process(Process-13, stopped)>, <Process(Process-14, stopped)>, <Process(Process-15, stopped)>, <Process(Process-16, stopped)>, <Process(Process-17, started)>, <Process(Process-18, started)>, <Process(Process-19, started)>, <Process(Process-20, started)>, <Process(Process-21, started)>, <Process(Process-22, started)>]
[[0]]
10
5
10
[<Process(Process-13, stopped)>, <Process(Process-14, stopped)>, <Process(Process-15, stopped)>, <Process(Process-16, stopped)>, <Process(Process-17, stopped)>, <Process(Process-18, stopped)>, <Process(Process-19, started)>, <Process(Process-20, started)>, <Process(Process-21, started)>, <Process(Process-22, started)>]
[[0]]
10
6
10
[<Process(Process-13, stopped)>, <Process(Process-14, stopped)>, <Process(Process-15, stopped)>, <Process(Process-16, stopped)>, <Process(Process-17, stopped)>, <Process(Process-18, stopped)>, <Process(Process-19, started)>, <Process(Process-20, started)>, <Process(Process-21, started)>, <Process(Process-22, started)>]
[[0]]
10
7
10
[<Process(Process-13, stopped)>, <Process(Process-14, stopped)>, <Process(Process-15, stopped)>, <Process(Process-16, stopped)>, <Process(Process-17, stopped)>, <Process(Process-18, stopped)>, <Process(Process-19, stopped)>, <Process(Process-20, started)>, <Process(Process-21, stopped)>, <Process(Process-22, started)>]
[[0]]
10
8
10
[<Process(Process-13, stopped)>, <Process(Process-14, stopped)>, <Process(Process-15, stopped)>, <Process(Process-16, stopped)>, <Process(Process-17, stopped)>, <Process(Process-18, stopped)>, <Process(Process-19, stopped)>, <Process(Process-20, stopped)>, <Process(Process-21, stopped)>, <Process(Process-22, started)>]
[[0]]
10
9
10
[<Process(Process-13, stopped)>, <Process(Process-14, stopped)>, <Process(Process-15, stopped)>, <Process(Process-16, stopped)>, <Process(Process-17, stopped)>, <Process(Process-18, stopped)>, <Process(Process-19, stopped)>, <Process(Process-20, stopped)>, <Process(Process-21, stopped)>, <Process(Process-22, started)>]
[[0]]

Multi processing V2.

Altering Test Case Function- Test Case Function V2


In [37]:
def test_cases_fn_2(test_cases_dict):
    
    #test_cases_list = None
    
    input_case = None
    
    test_cases_list = test_cases_list[input_case] = list()
            
    input_case =  'zero_element'
    
    test_cases_list.append(random_test_data(-1000,1000,0))
    
    test_cases_dict[input_case] = test_cases_list
    
    input_case = 'single_element'
    
    test_cases_list.append(random_test_data(-1000,1000,1))
    
    test_cases_dict[input_case] = test_cases_list
    
    input_case = 'double_element'
    test_cases_list.append(random_test_data(-1000,1000,2))
    
    test_cases_dict[input_case] = test_cases_list
    
    input_case = 'simple_case_positive'
    
    test_cases_list.append(random_test_data(0,1000,10))
    
    test_cases_dict[input_case] = test_cases_list
    
    input_case = 'simple_case_negative'
    
    test_cases_list.append(random_test_data(-1000,0,10))
    
    test_cases_dict[input_case] = test_cases_list
    
    input_case = 'simple_case_all'
    
    test_cases_list.append(random_test_data(-1000,1000,10))
    
    test_cases_dict[input_case] = test_cases_list
    
    input_case = 'medium_case_positive'
    
    test_cases_list.append(random_test_data(0,1000,100))
    
    test_cases_dict[input_case] = test_cases_list
    
    input_case = 'medium_case_negative'
    
    test_cases_list.append(random_test_data(-1000,0,100))
    
    test_cases_dict[input_case] = test_cases_list
    
    input_case = 'medium_case_all'
    
    test_cases_list.append(random_test_data(-1000,1000,100))
    
    test_cases_dict[input_case] = test_cases_list
    
    input_case = 'large_case_positive'
    
    test_cases_list.append(random_test_data(0,1000,1000))
    
    test_cases_dict[input_case] = test_cases_list
    
    input_case = 'large_case_negative'
    
    test_cases_list.append(random_test_data(-1000,0,1000))
    
    test_cases_dict[input_case] = test_cases_list
    
    input_case = 'large_case_all'
    
    test_cases_list.append(random_test_data(-1000,1000,1000))
    
    test_cases_dict[input_case] = test_cases_list
    
    input_case = 'extra_large_case_positive'
    
    test_cases_list.append(random_test_data(0,10000,10000))
    
    test_cases_dict[input_case] = test_cases_list
    
    input_case = 'extra_large_case_negative'
    
    test_cases_list.append(random_test_data(-10000,0,10000))
    
    test_cases_dict[input_case] = test_cases_list
    
    input_case = 'extra_large_case_all'
    
    test_cases_list.append(random_test_data(-10000,10000,10000))
    
    test_cases_dict[input_case] = test_cases_list
    
    input_case = 'binary_case_list_positive'
    
    test_cases_list.append(gen_list(0,1,10))
    
    test_cases_dict[input_case] = test_cases_list
    
    input_case = 'binary_case_negative'
    
    test_cases_list.append(gen_list(-1,0,10))
    
    test_cases_dict[input_case] = test_cases_list
    
    input_case = 'binary_case_all'
    
    test_cases_list.append(gen_list(-1,1,10))
    
    test_cases_dict[input_case] = test_cases_list
    
    input_case = 'simple_pyramid_case_positive'
    
    pyramid_arr = random_test_data(0,1000,10)
    
    test_cases_list.append( (list(reversed(pyramid_arr)))[:-1] + pyramid_arr )
    
    test_cases_dict[input_case] = test_cases_list
    
    input_case = 'simple_pyramid_case_negative'
    
    pyramid_arr = random_test_data(-1000,0,10)
    
    test_cases_list.append( (list(reversed(pyramid_arr)))[:-1] + pyramid_arr )
    
    test_cases_dict[input_case] = test_cases_list
    
    input_case = 'simple_pyramid_case_all'
    
    pyramid_arr = random_test_data(-1000,1000,10)
    
    test_cases_list.append( (list(reversed(pyramid_arr)))[:-1] + pyramid_arr )
    
    test_cases_dict[input_case] = test_cases_list
    
    input_case = 'medium_pyramid_case_positive'
    
    pyramid_arr = random_test_data(0,1000,100)
    
    test_cases_list.append( (list(reversed(pyramid_arr)))[:-1] + pyramid_arr )
    
    test_cases_dict[input_case] = test_cases_list
    
    input_case = 'medium_pyramid_case_negative'
    
    pyramid_arr = random_test_data(-1000,0,100)
    
    test_cases_list.append( (list(reversed(pyramid_arr)))[:-1] + pyramid_arr )
    
    test_cases_dict[input_case] = test_cases_list
    
    input_case = 'medium_pyramid_case_all'
    
    pyramid_arr = random_test_data(-1000,1000,100)
    
    test_cases_list.append( (list(reversed(pyramid_arr)))[:-1] + pyramid_arr )
    
    test_cases_dict[input_case] = test_cases_list
    
    input_case = 'large_pyramid_case_positive'
    
    pyramid_arr = random_test_data(0,1000,1000)
    
    test_cases_list.append( (list(reversed(pyramid_arr)))[:-1] + pyramid_arr )
    
    test_cases_dict[input_case] = test_cases_list
    
    input_case = 'large_pyramid_case_negative'
    
    pyramid_arr = random_test_data(-1000,0,1000)
    
    test_cases_list.append( (list(reversed(pyramid_arr)))[:-1] + pyramid_arr )
    
    test_cases_dict[input_case] = test_cases_list
    
    input_case = 'large_pyramid_case_all'
    
    pyramid_arr = random_test_data(-1000,1000,1000)
    
    test_cases_list.append( (list(reversed(pyramid_arr)))[:-1] + pyramid_arr )
    
    test_cases_dict[input_case] = test_cases_list
    
    input_case = 'simple_1_case__positive'
    
    test_cases_list.append(gen_list(1,1,10))
    
    test_cases_dict[input_case] = test_cases_list
    
    input_case = 'simple_1_case_negative'
    
    test_cases_list.append(gen_list(-1,-1,10))
    
    test_cases_dict[input_case] = test_cases_list
    
    input_case = 'medium_1_case_positive'
    
    test_cases_list.append(gen_list(1,1,100))
    
    test_cases_dict[input_case] = test_cases_list
    
    input_case = 'medium_1_case_negative'
    
    test_cases_list.append(gen_list(-1,1,100))
    
    test_cases_dict[input_case] = test_cases_list
    
    input_case = 'large_1_case_positive'
    
    test_cases_list.append(gen_list(1,1,1000))
    
    test_cases_dict[input_case] = test_cases_list
    
    input_case = 'large_1_case_negative'
    
    test_cases_list.append(gen_list(-1,-1,1000))
    
    test_cases_dict[input_case] = test_cases_list
    
    input_case = 'extra_large_1_case_positive'
    
    test_cases_list.append(gen_list(1,1,10000))
    
    test_cases_dict[input_case] = test_cases_list
    
    input_case = 'extra_large_1_case_negative'
    
    test_cases_list.append(gen_list(-1,-1,10000))
    
    test_cases_dict[input_case] = test_cases_list

Multiprocessing V2. Tricking the Manager to run as a dict() , while storing list() within it.


In [38]:
def mysubprocess(shared_dict):
    item = shared_dict['list_item'] = list()
    item.append('test')
    shared_dict['list_item'] = item
    print 'subprocess:', shared_dict





def test_cases_fn_3(test_cases_dict):
    
    input_case =  'zero_element'
    
    print random_test_data(-1000,1000,0)
    
    item = test_cases_dict[input_case] = list()
    
    item.append([random_test_data(-1000,1000,0)])
    
    test_cases_dict[input_case] = item
    
    print test_cases_dict
    
    #test_cases_list = random_test_data(-1000,1000,0)
    
    #input_case =  'zero_element'
    
    #test_cases_dict[input_case] = test_cases_list
            
    #input_case =  'zero_element'
    
    #test_cases_list.append(random_test_data(-1000,1000,0))
    
    #test_cases_dict[input_case] = test_cases_list

In [39]:
counter = 1

from multiprocessing import Lock,Queue,Process, current_process, Manager



if __name__ == '__main__':
    
    manager1 = Manager()
    
    queue_1 = Queue()
    
    test_cases_dict = manager1.dict()
    
    
    p_process_list = list()
    for count in xrange(counter):
        
        p_process = Process(target= , args= (test_cases_dict,))
        
        p_process.start()
        
        p_process_list.append(p_process)
            
        p_counter = 0
        
        print len(p_process_list)
        
        #test_cases_dict.clear()
   
    for count in xrange(counter):
        
        print counter
        
        print p_counter
        
        print len(p_process_list)
        
        print p_process_list
        
        p_process_list[p_counter].join()
        #test_cases_dict.clear()
        
        p_counter += 1
        
        print test_cases_dict


  File "<ipython-input-39-e8030a12082f>", line 19
    p_process = Process(target= , args= (test_cases_dict,))
                                ^
SyntaxError: invalid syntax

In [40]:
print random_test_data(0,10,10)


[5, 8, 4, 2, 3, 9, 0, 6, 1, 7]

Multi Processing V3.


In [71]:
counter = 10

from multiprocessing import Lock,Queue,Process, current_process, Manager

from random import randint

def gen_list_mp(start, stop ,
                input_case_size_name,
                input_case_type_name,
                test_cases_list):
    
    from random import randint
    
    values_list = list()
    
    input_case_size = { 
        
        "zero" : 0,
        
        "single" : 1,
        
        "double" : 2,
        
        "simple" : 10,
        
        "medium" : 100,
        
        "large" : 1000,
        
        "extra_large" : 10000
    
    }
    
    
    input_case_type= {
        
        "pyramid" : 1,
        
        "none" : 0
    }
    
    #from random import randint
    
    #values_list = list()
    
    for num in xrange(input_case_size[input_case_size_name]):
        values_list.append(randint(start,stop))
    
    if input_case_type[input_case_type_name] == 1:
        arr = values_list
        
        #pyramid_arr = list()
        
        values_list = (
            (list(reversed(arr)))[:-1] + arr )
    
        
    test_cases_list.append(values_list)
    

if __name__ == '__main__':
    
    manager1 = Manager()
    
    queue_1 = Queue()
    
    test_cases_list = manager1.list()
    
    
    p_process_list = list()
    for count in xrange(counter):
        
        p_process = Process(target= gen_list_mp , args= (1,10,"simple","none",test_cases_list))
        
        p_process.start()
        
        p_process_list.append(p_process)
            
        p_counter = 0
        
        print len(p_process_list)
        
        #test_cases_dict.clear()
   
    for count in xrange(counter):
        
        print counter
        
        print p_counter
        
        print len(p_process_list)
        
        print p_process_list
        
        p_process_list[p_counter].join()
        #test_cases_dict.clear()
        
        p_counter += 1
        
print test_cases_list


1
2
3
4
5
6
7
8
9
10
10
0
10
[<Process(Process-60, stopped)>, <Process(Process-61, stopped)>, <Process(Process-62, stopped)>, <Process(Process-63, stopped)>, <Process(Process-64, stopped)>, <Process(Process-65, stopped)>, <Process(Process-66, stopped)>, <Process(Process-67, stopped)>, <Process(Process-68, started)>, <Process(Process-69, started)>]
10
1
10
[<Process(Process-60, stopped)>, <Process(Process-61, stopped)>, <Process(Process-62, stopped)>, <Process(Process-63, stopped)>, <Process(Process-64, stopped)>, <Process(Process-65, stopped)>, <Process(Process-66, stopped)>, <Process(Process-67, stopped)>, <Process(Process-68, started)>, <Process(Process-69, started)>]
10
2
10
[<Process(Process-60, stopped)>, <Process(Process-61, stopped)>, <Process(Process-62, stopped)>, <Process(Process-63, stopped)>, <Process(Process-64, stopped)>, <Process(Process-65, stopped)>, <Process(Process-66, stopped)>, <Process(Process-67, stopped)>, <Process(Process-68, started)>, <Process(Process-69, started)>]
10
3
10
[<Process(Process-60, stopped)>, <Process(Process-61, stopped)>, <Process(Process-62, stopped)>, <Process(Process-63, stopped)>, <Process(Process-64, stopped)>, <Process(Process-65, stopped)>, <Process(Process-66, stopped)>, <Process(Process-67, stopped)>, <Process(Process-68, started)>, <Process(Process-69, started)>]
10
4
10
[<Process(Process-60, stopped)>, <Process(Process-61, stopped)>, <Process(Process-62, stopped)>, <Process(Process-63, stopped)>, <Process(Process-64, stopped)>, <Process(Process-65, stopped)>, <Process(Process-66, stopped)>, <Process(Process-67, stopped)>, <Process(Process-68, started)>, <Process(Process-69, started)>]
10
5
10
[<Process(Process-60, stopped)>, <Process(Process-61, stopped)>, <Process(Process-62, stopped)>, <Process(Process-63, stopped)>, <Process(Process-64, stopped)>, <Process(Process-65, stopped)>, <Process(Process-66, stopped)>, <Process(Process-67, stopped)>, <Process(Process-68, started)>, <Process(Process-69, started)>]
10
6
10
[<Process(Process-60, stopped)>, <Process(Process-61, stopped)>, <Process(Process-62, stopped)>, <Process(Process-63, stopped)>, <Process(Process-64, stopped)>, <Process(Process-65, stopped)>, <Process(Process-66, stopped)>, <Process(Process-67, stopped)>, <Process(Process-68, started)>, <Process(Process-69, started)>]
10
7
10
[<Process(Process-60, stopped)>, <Process(Process-61, stopped)>, <Process(Process-62, stopped)>, <Process(Process-63, stopped)>, <Process(Process-64, stopped)>, <Process(Process-65, stopped)>, <Process(Process-66, stopped)>, <Process(Process-67, stopped)>, <Process(Process-68, started)>, <Process(Process-69, started)>]
10
8
10
[<Process(Process-60, stopped)>, <Process(Process-61, stopped)>, <Process(Process-62, stopped)>, <Process(Process-63, stopped)>, <Process(Process-64, stopped)>, <Process(Process-65, stopped)>, <Process(Process-66, stopped)>, <Process(Process-67, stopped)>, <Process(Process-68, started)>, <Process(Process-69, started)>]
10
9
10
[<Process(Process-60, stopped)>, <Process(Process-61, stopped)>, <Process(Process-62, stopped)>, <Process(Process-63, stopped)>, <Process(Process-64, stopped)>, <Process(Process-65, stopped)>, <Process(Process-66, stopped)>, <Process(Process-67, stopped)>, <Process(Process-68, stopped)>, <Process(Process-69, started)>]
[[10, 6, 3, 10, 5, 9, 3, 2, 9, 7], [9, 1, 5, 5, 2, 2, 8, 3, 5, 5], [1, 10, 9, 2, 1, 7, 1, 9, 10, 8], [4, 2, 3, 3, 1, 4, 8, 3, 8, 6], [5, 3, 8, 2, 7, 7, 10, 10, 6, 5], [8, 3, 5, 6, 7, 7, 6, 8, 5, 7], [3, 1, 2, 2, 6, 6, 8, 7, 4, 5], [1, 6, 9, 10, 3, 2, 1, 3, 1, 5], [3, 8, 5, 7, 7, 7, 9, 6, 9, 1], [2, 6, 7, 5, 6, 5, 6, 3, 5, 7]]

In [42]:
print gen_list(0,0,0)


[]

In [43]:
test_dict = {"zero_element" : random_test_data(-1000,1000,0)}

In [44]:
a = test_dict["zero_element"]

print a


[]

In [49]:
test_dict_1 = { "test": [1]}

print (test_dict_1 ["test"] [0])


1

In [54]:
test_arr = [1,2,3,4,5,6,7]
 
print test_arr

arr = test_arr
        
pyramid_arr = list()
        
pyramid_arr = (
            (list(reversed(arr)))[:-1] + arr )

print pyramid_arr


[1, 2, 3, 4, 5, 6, 7]
[7, 6, 5, 4, 3, 2, 1, 2, 3, 4, 5, 6, 7]

MP V3.1


In [91]:
def tape_int_2_3_2(data):
    
    sums_list = list()
    
    if len(data) == 0:
        return 0
    
    elif len(data) == 1:
        left_side_sum = 0
        
        right_side_sum = data[0]
    else:
    
        left_side_sum = data[0]
        
        right_side_sum = sum(data[1:])
    
    min_diff = abs(left_side_sum - right_side_sum)
    
    sums_list.append([left_side_sum,right_side_sum,min_diff])
    
    pre_num = 0
    
    for i,num in enumerate(data):
        if i < 2:
            pre_num = num
            continue
        
        left_side_sum += pre_num
        
        right_side_sum -= pre_num
        
        current_diff = abs( left_side_sum - right_side_sum)
        
        sums_list.append([left_side_sum,right_side_sum,current_diff])
        
        pre_num = num
                           
        if min_diff > current_diff:
            min_diff = current_diff
    
    sums_list.append(min_diff)
            
    return sums_list

In [97]:
counter = 10

from multiprocessing import Lock,Queue,Process, current_process, Manager

from random import randint

def gen_list_mp(start, stop ,
                input_case_size_name,
                input_case_type_name,
                test_cases_list):
    
    from random import randint
    
    values_list = list()
    
    input_case_size = { 
        
        "zero" : 0,
        
        "single" : 1,
        
        "double" : 2,
        
        "simple" : 10,
        
        "medium" : 100,
        
        "large" : 1000,
        
        "extra_large" : 10000
    
    }
    
    
    input_case_type= {
        
        "pyramid" : 1,
        
        "none" : 0
    }
    
 
   
    for num in xrange(input_case_size[input_case_size_name]):
        values_list.append(randint(start,stop))
    
    if input_case_type[input_case_type_name] == 1:
        arr = values_list
        
        #pyramid_arr = list()
        
        values_list = (
            (list(reversed(arr)))[:-1] + arr )
    
        
    test_cases_list.append(values_list)
    

if __name__ == '__main__':
    
    manager1 = Manager()
    
    queue_1 = Queue()
    
    test_cases_list = manager1.list()
    
    
    p_process_list = list()
    for count in xrange(counter):
        
        p_process = Process(target= gen_list_mp , args= (1,10,"simple","pyramid",test_cases_list))
        
        p_process.start()
        
        p_process_list.append(p_process)
            
        p_counter = 0
           
    for count in xrange(counter):
        p_process_list[p_counter].join()
        
        p_counter += 1
        
print test_cases_list


[[6, 3, 2, 9, 1, 5, 5, 8, 7, 5, 7, 8, 5, 5, 1, 9, 2, 3, 6], [2, 4, 4, 2, 4, 1, 9, 4, 5, 2, 5, 4, 9, 1, 4, 2, 4, 4, 2], [6, 8, 8, 10, 3, 9, 1, 7, 9, 4, 9, 7, 1, 9, 3, 10, 8, 8, 6], [7, 10, 1, 6, 1, 7, 10, 9, 1, 7, 1, 9, 10, 7, 1, 6, 1, 10, 7], [5, 8, 1, 9, 3, 9, 8, 8, 6, 10, 6, 8, 8, 9, 3, 9, 1, 8, 5], [10, 4, 8, 6, 10, 5, 10, 8, 6, 9, 6, 8, 10, 5, 10, 6, 8, 4, 10], [2, 1, 5, 2, 3, 4, 7, 2, 1, 3, 1, 2, 7, 4, 3, 2, 5, 1, 2], [5, 10, 7, 4, 6, 5, 4, 7, 9, 9, 9, 7, 4, 5, 6, 4, 7, 10, 5], [10, 2, 10, 4, 7, 4, 5, 5, 3, 7, 3, 5, 5, 4, 7, 4, 10, 2, 10], [4, 8, 2, 5, 6, 1, 9, 1, 1, 2, 1, 1, 9, 1, 6, 5, 2, 8, 4]]

In [106]:
test_tape = list()

for case in test_cases_list:
    #print case

    test_tape.append(tape_int_2_3_2(case))
    
print test_tape[0]

    

#print test_tape[:-1]


[[6, 91, 85], [9, 88, 79], [11, 86, 75], [20, 77, 57], [21, 76, 55], [26, 71, 45], [31, 66, 35], [39, 58, 19], [46, 51, 5], [51, 46, 5], [58, 39, 19], [66, 31, 35], [71, 26, 45], [76, 21, 55], [77, 20, 57], [86, 11, 75], [88, 9, 79], [91, 6, 85], 5]

Linear Regression


In [ ]:
def lin_reg(data):
    """
    Analysis Training set results vs testing results
    :return: 
    """

I believe If i apply K-neareast neahbour to test_tape[0] and apply linear regression to the over all test sample. I should be receiving a decent prediction score. Moving on for now.To be continued.


In [ ]: